<template>
  <cement-content
    :title="boreholeTitle"
    :expandTitle="boreholeExpandTitle"
    :search="boreholeSearch"
    @getData="getBorehole"
    @deleteRow="deleteBorehole"
    @submitUpdate="submitBoreholeUpdate"
    @submitSearchModel="submitBoreholeSearchModel">
  </cement-content>
</template>

<script>
    import cementContent from '@/components/cement-content'
    import {BOREHOLE_TITLE, EXPAND_BOREHOLE_TITLE, BOREHOLE_SEARCH_DATA, SEPARATOR, FIRST_PAGE} from '@/util/constant'
    import {mapGetters, mapMutations} from 'vuex'
    import {getBorehole, deleteWell, updateWell} from '@/api/module/borehole'
    import {get, cloneDeep} from 'lodash'

    export default {
        name: 'index',
        components: {
            cementContent
        },
        data() {
            return {
                boreholeTitle: BOREHOLE_TITLE,
                boreholeExpandTitle: EXPAND_BOREHOLE_TITLE,
                boreholeSearch: BOREHOLE_SEARCH_DATA
            }
        },
        methods: {
            ...mapMutations([
                'changeTableData',
                'changeTotal',
                'closeDialog',
                'changePageSize'
            ]),
            getBorehole() {
                let search = cloneDeep(this.searchModelGetters)
                if (get(this.searchModelGetters, 'drillDate')) {
                    search['drillDate'] = this.searchModelGetters['drillDate'][0] + SEPARATOR + this.searchModelGetters['drillDate'][1]
                }
                getBorehole(this.currentPageGetters - 1, this.pageSizeGetters, search).then((res) => {
                    this.changeTableData(get(res, 'data.data.content', []))
                    this.changeTotal(get(res, 'data.data.totalElements', 0))
                })
            },
            deleteBorehole(row) {
                if (get(row, 'id')) {
                    deleteWell(row['id'])
                }
            },
            submitBoreholeSearchModel() {
                this.changePageSize(FIRST_PAGE)
                this.getBorehole()
            },
            submitBoreholeUpdate() {
                if (get(this.updateModelGetters, 'wellId')) {
                    updateWell(get(this.updateModelGetters, 'wellId'), this.updateModelGetters).then(
                        this.closeDialog()
                    )
                }
            }
        },
        computed: {
            ...mapGetters([
                'pageSizeGetters',
                'currentPageGetters',
                'searchModelGetters',
                'updateModelGetters'
            ])
        },
        mounted() {
            this.getBorehole()
        }
    }
</script>
